import { _decorator, Component, SpriteFrame, Sprite, UITransform } from "cc";
const {ccclass } = _decorator;

export enum TileType {
    WALL_ROW = 1,
    WALL_COLUMN = 2,
    WALL_LEFT_TOP = 3,
    WALL_RIGHT_TOP = 4,
    WALL_LEFT_BOTTOM = 5,
    WALL_RIGHT_BOTTOM = 6,
    CLIFF_LEFT = 7,
    CLIFF_CENTER = 8,
    CLIFF_RIGHT = 9,
    FLOOR = 10,
  }
  

@ccclass("Tile")
export class Tile extends Component {

    public static readonly Type = TileType;

    public type: TileType;
    public moveable: boolean = true;
    public turnable: boolean = true;

    init(type: TileType, spriteFrame: SpriteFrame, row: number, column: number): void {
        this.type = type;
        this.calculate();

        let transform = this.getComponent(UITransform);
        let sprite = this.getComponent(Sprite);
        sprite.spriteFrame = spriteFrame;
        this.node.setPosition(row * transform.width, column * transform.height);
    }

    private calculate(): void {
        if (
            this.type === Tile.Type.WALL_LEFT_TOP ||
            this.type === Tile.Type.WALL_ROW ||
            this.type === Tile.Type.WALL_RIGHT_TOP ||
            this.type === Tile.Type.WALL_LEFT_BOTTOM ||
            this.type === Tile.Type.WALL_RIGHT_BOTTOM ||
            this.type === Tile.Type.WALL_COLUMN
          ) {
            this.moveable = false;
            this.turnable = false;
          } else if (
            this.type === Tile.Type.CLIFF_LEFT ||
            this.type === Tile.Type.CLIFF_CENTER ||
            this.type === Tile.Type.CLIFF_RIGHT
          ) {
            this.moveable = false;
            this.turnable = true;
          } else if (this.type === Tile.Type.FLOOR) {
            this.moveable = true;
            this.turnable = true;
          }
    }


}